package it.unina.lusi;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.PointF;
import android.view.MotionEvent;
import android.widget.Toast;

import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

public class MyOverlay extends Overlay  {

	private ArrayList<PointF> touchPoints = null;
	private Paint drawingPaint = null;
	private Projection projection = null;
	private Path percorso = null;
	private Context context = null;
	private PointF start = null;
	
	public MyOverlay(MapView mapView, Context context){
        projection = mapView.getProjection();
        this.context = context;
        
        drawingPaint = new Paint();
		drawingPaint.setColor(Color.RED);
		drawingPaint.setStrokeWidth(10);
		drawingPaint.setStyle(Paint.Style.STROKE);
		drawingPaint.setAntiAlias(true);
		touchPoints = new ArrayList<PointF>();
        
    }
	
	
	public void draw(Canvas canvas, MapView mapView, boolean shadow){
        super.draw(canvas, mapView, shadow);
        //if(touchPoints.size() >= 5){
      /*  	float[] points = new float[touchPoints.size()*2];
        	int i = 0;
        	for(PointF point : touchPoints){
        		points[i] = point.x;
        		points[i++] = point.y;
        		i++;
        	} */
       
        if(touchPoints.size() > 0) {
        	 percorso = new Path();	
             percorso.moveTo(start.x, start.y);
        	for(PointF point : touchPoints){
        		percorso.lineTo(point.x, point.y);
        	}
        	
        	canvas.drawPath(percorso,drawingPaint);
        	//canvas.drawLines(points, 0, touchPoints.size(), drawingPaint);
        	//canvas.drawLine(touchPoints.get(0).x, touchPoints.get(0).y, touchPoints.get(touchPoints.size()-1).x, touchPoints.get(touchPoints.size()-1).y, drawingPaint);
        }	
        
      }	
	
	
	 @Override
     public boolean onTouchEvent(MotionEvent event, MapView mapView) 
     {   
         //---when user lifts his finger---
      /*   if (event.getAction() == 1) {                
             GeoPoint p = mapView.getProjection().fromPixels(
                 (int) event.getX(),
                 (int) event.getY());
                 Toast.makeText(context, 
                     p.getLatitudeE6() / 1E6 + "," + 
                     p.getLongitudeE6() /1E6 , 
                     Toast.LENGTH_SHORT).show();
         }   */     
         
		 
				int action = event.getAction() & MotionEvent.ACTION_MASK;

				switch(action)
				{
					case MotionEvent.ACTION_DOWN:
					{
						//Toast.makeText(context, "Action_down" , Toast.LENGTH_SHORT).show();
						touchPoints.clear();
						start = new PointF(event.getX(), event.getY());
						//touchPoints.add(new PointF(event.getX(),event.getY()));
						break;
					}
					case MotionEvent.ACTION_POINTER_DOWN:
					{
						//Toast.makeText(context, "Action_Pointer_down" , Toast.LENGTH_SHORT).show();
						touchPoints.add(new PointF(event.getX(),event.getY()));				
						break;
					}
					case MotionEvent.ACTION_UP:
					{
				
						touchPoints.add(new PointF(event.getX(),event.getY()));
						Toast.makeText(context, "Action_Pointer_up size: " + touchPoints.size() , Toast.LENGTH_SHORT).show();
						mapView.invalidate();
						break;
					}
					case MotionEvent.ACTION_MOVE:
					{
						//Toast.makeText(context, "Action_move" , Toast.LENGTH_SHORT).show();
						touchPoints.add(new PointF(event.getX(),event.getY()));
						break;
					}
				} 
				
         return true;
	 
	 }

	
	
}
